//
//  CmsScrollView.m
//  Brand
//
//  Created by Faker on 4/29/14.
//  Copyright (c) 2014 Faker. All rights reserved.
//

#import "CmsScrollView.h"
#import "UIView+EidanSize.h"

@interface CmsScrollView()

@property (assign, nonatomic) float topScrollWidth;

@end

@implementation CmsScrollView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.topScrollWidth = frame.size.width/3;
        self.topScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(self.topScrollWidth, 0, self.topScrollWidth, 100)];
        //self.topScrollView.decelerationRate = 0.5;
        self.topScrollView.showsHorizontalScrollIndicator = NO;
        self.topScrollView.showsVerticalScrollIndicator = NO;
        self.topScrollView.clipsToBounds = NO; //超出可见
        self.topScrollView.contentWidth = self.topScrollWidth*5; //5个大小
        self.topScrollView.contentOffsetLeft = self.topScrollWidth*2;
        self.topScrollView.pagingEnabled = YES;
        self.topScrollView.delegate = self;
        [self addSubview:self.topScrollView];
        
        self.curPage = 0;
       self.clipsToBounds = YES; //超出不可见
    }
    return self;
}


-(void)reloadData
{
    self.totalPages = [self.dataSource numberOfPages];
    if(self.totalPages==0)
    {
        return;
    }
    [self loadData];
}

-(void)loadData
{
    //每次刷新的时候要清空所有视图
    [self.topScrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self getNeedViewsWithCurpage:self.curPage];
    
    for (int i = 0; i<5; i++) {
        UIView* mView = [self.topViews objectAtIndex:i];
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                    action:@selector(handleTap:)];
        [mView addGestureRecognizer:singleTap];
        mView.frame = CGRectOffset(mView.frame, self.topScrollWidth*i, 0);
        [self.topScrollView addSubview:mView];
    }
    self.topScrollView.contentOffsetLeft = self.topScrollWidth*2;
}

-(void)handleTap:(UITapGestureRecognizer*)tap
{
    NSLog(@"dataSource = %@",self.dataSource);
    NSLog(@"delegate = %@",self.delegate);
    if([self.delegate respondsToSelector:@selector(cmsScrollView:didSelectItemAtIndex:)])
    {
        [self.delegate cmsScrollView:self didSelectItemAtIndex:self.curPage];
    }
}

#pragma mark - 剧中
-(void)handleCenter
{
    [self.topScrollView setContentOffset:CGPointMake(self.topScrollWidth, 0) animated:YES];
}

//根据当前的页码得到需要显示的5张图
-(void)getNeedViewsWithCurpage:(int)curPage
{
    int ppre = [self validPageValue:self.curPage-2];
    int pre  = [self validPageValue:self.curPage-1];
    int last = [self validPageValue:self.curPage+1];
    int llast = [self validPageValue:self.curPage+2];
    if(!self.topViews)
    {
        self.topViews = [[NSMutableArray alloc]init];
    }
    [self.topViews removeAllObjects];
    [self.topViews addObject:[self.dataSource pageAtIndex:ppre]];
    [self.topViews addObject:[self.dataSource pageAtIndex:pre]];
    [self.topViews addObject:[self.dataSource pageAtIndex:curPage]];
    [self.topViews addObject:[self.dataSource pageAtIndex:last]];
    [self.topViews addObject:[self.dataSource pageAtIndex:llast]];

}

-(int)validPageValue:(int)value
{
    if(value == -1) value = self.totalPages - 1;
    if(value == -2) value = self.totalPages - 2;
    if(value == self.totalPages) value = 0;
    if(value == self.totalPages + 1) value = 1;
    return value;
}

#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    
    float x = scrollView.contentOffsetLeft;
//    NSLog(@"x = %f",x);
    if(x >=3*self.topScrollWidth)
    {
       self.curPage = [self validPageValue:self.curPage+1];
        [self loadData];
    }
    
    if(x<=self.topScrollWidth)
    {
        self.curPage = [self validPageValue:self.curPage-1];
        [self loadData];
    }
    
    
}





@end
